package com.afs.electronicforensics.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.afs.electronicforensics.adapter.RecordCloudAdapter;
import com.afs.electronicforensics.adapter.VideoCloudAdapter;
import com.afs.electronicforensics.entity.FileInfo;
import com.afs.electronicforensics.activity.R;
import com.afs.electronicforensics.entity.MessagePage;
import com.afs.electronicforensics.utils.Constants;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import static android.widget.NumberPicker.OnScrollListener.SCROLL_STATE_IDLE;

public class RecordCloudFragment extends Fragment {
    private String myUrl;
    ArrayList<FileInfo> items = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.recyclerview_card, container, false);

        myUrl = Constants.URL_ROOT+"file/findAll?page=0&size=500&type=3&userId="+ Constants.USER_ROOT.getUid();
        System.out.println(getActivity().getIntent().getStringExtra("name"));
        super.onResume();
        getJsonTask task2 = new getJsonTask(myUrl, root);
        task2.execute();   //发送请求

        EditText searchText = root.findViewById(R.id.search_text);
        ImageView searchBtn = root.findViewById(R.id.search_btn);

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                items.removeAll(items);
                myUrl = Constants.URL_ROOT+"file/findAll?name="+searchText.getText().toString().trim()+"&page=0&size=500&type=3&userId="+ Constants.USER_ROOT.getUid();
                getJsonTask task2 = new getJsonTask(myUrl, root);
                task2.execute();//发送请求
                System.out.println(myUrl);
            }
        });

        return root;
    }
    class getJsonTask extends AsyncTask<String, Object,ArrayList<FileInfo>> {
        String mUrl;
        ViewGroup root;

        public getJsonTask(String url,ViewGroup root){
            this.mUrl = url;
            this.root = root;
        }

        @Override
        protected  ArrayList<FileInfo> doInBackground(String... params) {
            URL url = null;
            try {
                url = new URL(mUrl);
                HttpURLConnection connection = null;
                connection = (HttpURLConnection) url.openConnection();
                InputStream stream = null;
                stream = connection.getInputStream();
                BufferedReader bufferedReader = null;
                bufferedReader = new BufferedReader(new InputStreamReader(stream,
                        "UTf-8"));
                String sread = null;
                StringBuffer buffer = new StringBuffer();
                while (true) {
                    try {
                        if (!((sread = bufferedReader.readLine()) != null)) break;
                    } catch (IOException e) {
                    e.printStackTrace();
                    }
                    buffer.append(sread);
                    buffer.append("\r\n");
                }
                String string = buffer.toString();
                if (string != null && string.startsWith("\ufeff")) {
                string = string.substring(1);
                }
                System.out.println(string);
                String str = "https://file-njtech.oss-cn-hangzhou.aliyuncs.com/";
                Gson gson = new Gson();
                MessagePage messagePage = gson.fromJson(string, MessagePage.class);
                if(null!=messagePage){
                    for(int i = 0;i<messagePage.getResult().size();i++)
                    {
                        items.add(new FileInfo(messagePage.getResult().get(i).getName(),str+messagePage.getResult().get(i).getUrl(),messagePage.getResult().get(i).getContent(),messagePage.getResult().get(i).getId()));
                    }
                }
                return items;
                } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(ArrayList<FileInfo> result) {
            // TODO Auto-generated method stub
            if(result!=null) {
                RecyclerView recyclerView = root.findViewById(R.id.recyclerview_card);
                StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                RecordCloudAdapter adapter = new RecordCloudAdapter(getContext(), items);
                recyclerView.setAdapter(adapter);
                super.onPostExecute(result);
            }
        }
    }
}
